/*
* @Author: An
* @Date:   2017-07-17 14:15:40
* @Last Modified by:   An
* @Last Modified time: 2017-07-17 22:14:57
*/

'use strict';
require('page/common/header/index.js');
require('page/common/nav/index.js');
require('./index.css');
var _xj 		  = require('util/xj.js');
var _payment 	  = require('service/payment-service.js');
var templateIndex = require('./index.string');

// page逻辑
var page = {
	data : {
		orderNumber : _xj.getUrlParam('orderNumber')
	},
	init : function() {
		this.onLoad();
	},
	onLoad : function() {
		this.loadPaymentInfo();
	},
	loadPaymentInfo : function() {
		var _this = this,
			paymentHtml = '',
			$pageWrap = $('.page-wrap');
		$pageWrap.html('<div class="loading"></div>');
		_payment.getPaymentInfo(this.data.orderNumber, function(res) {
			paymentHtml = _xj.renderHtml(templateIndex, res);
			$pageWrap.html(paymentHtml);
			_this.listenOrderStatus();
		}, function(errMsg) {
			$pageWrap.html('<p class="err-tip">' + errMsg + '</p>');
		});
	},
	// 监听订单状态
	listenOrderStatus : function() {
		var _this = this;
		this.paymentTimer = window.setInterval(function() {
			_payment.getPaymentStatus(_this.data.orderNumber, function(res) {
				if(res == true) {
					window.location.href = './result.html?type=payment&orderNumber=' + _this.data.orderNumber;
				}
			}, function(errMsg) {
				
			});
		}, 5e3);
	}
};

$(function() {
	page.init();
});